/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-12
*/
package com.rzico.core.controller;

import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.core.entity.SysPvuvTravel;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.SysPvuvSummaryService;
import com.rzico.core.service.SysPvuvTravelService;
import com.rzico.core.service.SysUserService;
import com.rzico.util.IPUtils;
import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * 访问记录控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-12
 */
@Api(description = "访问记录接口")
@RestController
@RequestMapping("/pvuv")
public class PvuvController extends BaseController {

    @Autowired
    private SysPvuvTravelService sysPvuvTravelService;

    @Autowired
    private SysUserService sysUserService;

    static UASparser uasParser = null;
    static {
        try {
            uasParser = new UASparser(OnlineUpdater.getVendoredInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 访问轨迹采集
     *
     * @param request
     * @return
     */
    @ApiOperation("访问轨迹采集")
    @PostMapping("/add")
    public CommResult<SysPvuvTravel> add(String mchId,String path,Integer pathType,Long dataId, HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        SysPvuvTravel pvuv = new SysPvuvTravel();
        pvuv.setClientIp(IPUtils.getIp(request));
        try {
            UserAgentInfo userAgentInfo = uasParser.parse(userAgent);
            pvuv.setDeviceType(userAgentInfo.getOsFamily());
            pvuv.setPlatform(userAgentInfo.getOsName());
            pvuv.setPath(path);
            pvuv.setPathType(pathType);
            pvuv.setDataId(dataId);
            pvuv.setMchId(mchId);
            Map<String,String> sysUser = sysUserService.getToken();
            if (sysUser!=null) {
                pvuv.setUserId(sysUser.get("id"));
            }
//            30 System.out.println("操作系统名称：" + userAgentInfo.getOsFamily());//
//            31 System.out.println("操作系统：" + userAgentInfo.getOsName());//
//            32 System.out.println("浏览器名称：" + userAgentInfo.getUaFamily());//
//            33 System.out.println("浏览器版本：" + userAgentInfo.getBrowserVersionInfo());//
//            34 System.out.println("设备类型：" + userAgentInfo.getDeviceType());
//            35 System.out.println("浏览器:" + userAgentInfo.getUaName());
//            36 System.out.println("类型：" + userAgentInfo.getType());
            int affectCount = sysPvuvTravelService.insertUseGeneratedKeys(pvuv);
            return CommResult.success("记录完成");
        } catch (Exception e) {
            return CommResult.success("记录失败");
        }
    }

}
